﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Core.Net;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.Keyence
{
	/// <summary>
	/// 基恩士KV上位链路串口通信的对象,适用于Nano系列串口数据,以及L20V通信模块，本类是基于tcp通信方式的类<br />
	/// Keyence KV upper link serial communication object, suitable for Nano series serial data and L20V communication module, this class is a class based on TCP communication method
	/// </summary>
	/// <remarks>
	/// 当读取Bool的输入的格式说明如下：
	/// <list type="table">
	///   <listheader>
	///     <term>地址名称</term>
	///     <term>地址代号</term>
	///     <term>示例</term>
	///     <term>地址范围</term>
	///     <term>备注</term>
	///   </listheader>
	///   <item>
	///     <term>继电器</term>
	///     <term>R</term>
	///     <term>R0,R100</term>
	///     <term>0-59915</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>控制继电器</term>
	///     <term>CR</term>
	///     <term>CR0,CR100</term>
	///     <term>0-3915</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>内部辅助继电器</term>
	///     <term>MR</term>
	///     <term>MR0,MR100</term>
	///     <term>0-99915</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>锁存继电器</term>
	///     <term>LR</term>
	///     <term>LR0,LR100</term>
	///     <term>0-99915</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>定时器</term>
	///     <term>T</term>
	///     <term>T0,T100</term>
	///     <term>0-3999</term>
	///     <term>通断</term>
	///   </item>
	///   <item>
	///     <term>计数器</term>
	///     <term>C</term>
	///     <term>C0,C100</term>
	///     <term>0-3999</term>
	///     <term>通断</term>
	///   </item>
	///   <item>
	///     <term>高速计数器</term>
	///     <term>CTH</term>
	///     <term>CTH0,CTH1</term>
	///     <term>0-1</term>
	///     <term>通断</term>
	///   </item>
	/// </list>
	/// 读取数据的地址如下：
	///
	/// <list type="table">
	///   <listheader>
	///     <term>地址名称</term>
	///     <term>地址代号</term>
	///     <term>示例</term>
	///     <term>地址范围</term>
	///     <term>备注</term>
	///   </listheader>
	///   <item>
	///     <term>数据存储器</term>
	///     <term>DM</term>
	///     <term>DM0,DM100</term>
	///     <term>0-65534</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>控制存储器</term>
	///     <term>CM</term>
	///     <term>CM0,CM100</term>
	///     <term>0-11998</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>临时数据存储器</term>
	///     <term>TM</term>
	///     <term>TM0,TM100</term>
	///     <term>0-511</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>扩展数据存储器</term>
	///     <term>EM</term>
	///     <term>EM0,EM100</term>
	///     <term>0-65534</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>扩展数据存储器</term>
	///     <term>FM</term>
	///     <term>FM0,FM100</term>
	///     <term>0-32766</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>变址寄存器</term>
	///     <term>Z</term>
	///     <term>Z1,Z5</term>
	///     <term>1-12</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>数字微调器</term>
	///     <term>AT</term>
	///     <term>AT0,AT5</term>
	///     <term>0-7</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>定时器</term>
	///     <term>T</term>
	///     <term>T0,T100</term>
	///     <term>0-3999</term>
	///     <term>当前值(current value), 读int</term>
	///   </item>
	///   <item>
	///     <term>计数器</term>
	///     <term>C</term>
	///     <term>C0,C100</term>
	///     <term>0-3999</term>
	///     <term>当前值(current value), 读int</term>
	///   </item>
	/// </list>
	/// </remarks>
	// Token: 0x0200008F RID: 143
	public class KeyenceNanoSerialOverTcp : NetworkDeviceSoloBase
	{
		/// <summary>
		/// 实例化一个默认的对象<br />
		/// Instantiate a default object
		/// </summary>
		// Token: 0x06000ADF RID: 2783 RVA: 0x00040A25 File Offset: 0x0003EC25
		public KeyenceNanoSerialOverTcp()
		{
			base.WordLength = 1;
			base.ByteTransform = new RegularByteTransform();
		}

		/// <summary>
		/// 使用指定的ip地址和端口号来初始化对象<br />
		/// Initialize the object with the specified IP address and port number
		/// </summary>
		/// <param name="ipAddress">Ip地址数据</param>
		/// <param name="port">端口号</param>
		// Token: 0x06000AE0 RID: 2784 RVA: 0x00040A43 File Offset: 0x0003EC43
		public KeyenceNanoSerialOverTcp(string ipAddress, int port)
		{
			base.WordLength = 1;
			this.IpAddress = ipAddress;
			this.Port = port;
			base.ByteTransform = new RegularByteTransform();
		}

		/// <inheritdoc />
		// Token: 0x06000AE1 RID: 2785 RVA: 0x00040A74 File Offset: 0x0003EC74
		protected override OperateResult InitializationOnConnect(Socket socket)
		{
			OperateResult<byte[]> operateResult = this.ReadFromCoreServer(socket, KeyenceNanoSerialOverTcp.ConnectCmd, true, true);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				result = OperateResult.CreateSuccessResult();
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000AE2 RID: 2786 RVA: 0x00040AAC File Offset: 0x0003ECAC
		protected override OperateResult ExtraOnDisconnect(Socket socket)
		{
			OperateResult<byte[]> operateResult = this.ReadFromCoreServer(socket, KeyenceNanoSerialOverTcp.DisConnectCmd, true, true);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				result = OperateResult.CreateSuccessResult();
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000AE3 RID: 2787 RVA: 0x00040AE4 File Offset: 0x0003ECE4
		[DebuggerStepThrough]
		protected override Task<OperateResult> InitializationOnConnectAsync(Socket socket)
		{
			KeyenceNanoSerialOverTcp.<InitializationOnConnectAsync>d__4 <InitializationOnConnectAsync>d__ = new KeyenceNanoSerialOverTcp.<InitializationOnConnectAsync>d__4();
			<InitializationOnConnectAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<InitializationOnConnectAsync>d__.<>4__this = this;
			<InitializationOnConnectAsync>d__.socket = socket;
			<InitializationOnConnectAsync>d__.<>1__state = -1;
			<InitializationOnConnectAsync>d__.<>t__builder.Start<KeyenceNanoSerialOverTcp.<InitializationOnConnectAsync>d__4>(ref <InitializationOnConnectAsync>d__);
			return <InitializationOnConnectAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000AE4 RID: 2788 RVA: 0x00040B30 File Offset: 0x0003ED30
		[DebuggerStepThrough]
		protected override Task<OperateResult> ExtraOnDisconnectAsync(Socket socket)
		{
			KeyenceNanoSerialOverTcp.<ExtraOnDisconnectAsync>d__5 <ExtraOnDisconnectAsync>d__ = new KeyenceNanoSerialOverTcp.<ExtraOnDisconnectAsync>d__5();
			<ExtraOnDisconnectAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<ExtraOnDisconnectAsync>d__.<>4__this = this;
			<ExtraOnDisconnectAsync>d__.socket = socket;
			<ExtraOnDisconnectAsync>d__.<>1__state = -1;
			<ExtraOnDisconnectAsync>d__.<>t__builder.Start<KeyenceNanoSerialOverTcp.<ExtraOnDisconnectAsync>d__5>(ref <ExtraOnDisconnectAsync>d__);
			return <ExtraOnDisconnectAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000AE5 RID: 2789 RVA: 0x00040B7C File Offset: 0x0003ED7C
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<byte[]> operateResult = KeyenceNanoSerialOverTcp.BuildReadCommand(address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
				}
				else
				{
					OperateResult operateResult3 = KeyenceNanoSerialOverTcp.CheckPlcReadResponse(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<byte[]>(operateResult3);
					}
					else
					{
						OperateResult<string, int> operateResult4 = KeyenceNanoSerialOverTcp.KvAnalysisAddress(address);
						bool flag4 = !operateResult4.IsSuccess;
						if (flag4)
						{
							result = OperateResult.CreateFailedResult<byte[]>(operateResult4);
						}
						else
						{
							result = KeyenceNanoSerialOverTcp.ExtractActualData(operateResult4.Content1, operateResult2.Content);
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000AE6 RID: 2790 RVA: 0x00040C30 File Offset: 0x0003EE30
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<byte[]> operateResult = KeyenceNanoSerialOverTcp.BuildWriteCommand(address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = KeyenceNanoSerialOverTcp.CheckPlcWriteResponse(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Keyence.KeyenceNanoSerialOverTcp.Read(System.String,System.UInt16)" />
		// Token: 0x06000AE7 RID: 2791 RVA: 0x00040CA4 File Offset: 0x0003EEA4
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
		{
			KeyenceNanoSerialOverTcp.<ReadAsync>d__8 <ReadAsync>d__ = new KeyenceNanoSerialOverTcp.<ReadAsync>d__8();
			<ReadAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadAsync>d__.<>4__this = this;
			<ReadAsync>d__.address = address;
			<ReadAsync>d__.length = length;
			<ReadAsync>d__.<>1__state = -1;
			<ReadAsync>d__.<>t__builder.Start<KeyenceNanoSerialOverTcp.<ReadAsync>d__8>(ref <ReadAsync>d__);
			return <ReadAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Keyence.KeyenceNanoSerialOverTcp.Write(System.String,System.Byte[])" />
		// Token: 0x06000AE8 RID: 2792 RVA: 0x00040CF8 File Offset: 0x0003EEF8
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, byte[] value)
		{
			KeyenceNanoSerialOverTcp.<WriteAsync>d__9 <WriteAsync>d__ = new KeyenceNanoSerialOverTcp.<WriteAsync>d__9();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<KeyenceNanoSerialOverTcp.<WriteAsync>d__9>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000AE9 RID: 2793 RVA: 0x00040D4C File Offset: 0x0003EF4C
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			OperateResult<byte[]> operateResult = KeyenceNanoSerialOverTcp.BuildReadCommand(address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<bool[]>(operateResult2);
				}
				else
				{
					OperateResult operateResult3 = KeyenceNanoSerialOverTcp.CheckPlcReadResponse(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<bool[]>(operateResult3);
					}
					else
					{
						OperateResult<string, int> operateResult4 = KeyenceNanoSerialOverTcp.KvAnalysisAddress(address);
						bool flag4 = !operateResult4.IsSuccess;
						if (flag4)
						{
							result = OperateResult.CreateFailedResult<bool[]>(operateResult4);
						}
						else
						{
							result = KeyenceNanoSerialOverTcp.ExtractActualBoolData(operateResult4.Content1, operateResult2.Content);
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000AEA RID: 2794 RVA: 0x00040E00 File Offset: 0x0003F000
		[HslMqttApi("WriteBool", "")]
		public override OperateResult Write(string address, bool value)
		{
			OperateResult<byte[]> operateResult = KeyenceNanoSerialOverTcp.BuildWriteCommand(address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = KeyenceNanoSerialOverTcp.CheckPlcWriteResponse(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Keyence.KeyenceNanoSerialOverTcp.ReadBool(System.String,System.UInt16)" />
		// Token: 0x06000AEB RID: 2795 RVA: 0x00040E74 File Offset: 0x0003F074
		[DebuggerStepThrough]
		public override Task<OperateResult<bool[]>> ReadBoolAsync(string address, ushort length)
		{
			KeyenceNanoSerialOverTcp.<ReadBoolAsync>d__12 <ReadBoolAsync>d__ = new KeyenceNanoSerialOverTcp.<ReadBoolAsync>d__12();
			<ReadBoolAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
			<ReadBoolAsync>d__.<>4__this = this;
			<ReadBoolAsync>d__.address = address;
			<ReadBoolAsync>d__.length = length;
			<ReadBoolAsync>d__.<>1__state = -1;
			<ReadBoolAsync>d__.<>t__builder.Start<KeyenceNanoSerialOverTcp.<ReadBoolAsync>d__12>(ref <ReadBoolAsync>d__);
			return <ReadBoolAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Keyence.KeyenceNanoSerialOverTcp.Write(System.String,System.Boolean)" />
		// Token: 0x06000AEC RID: 2796 RVA: 0x00040EC8 File Offset: 0x0003F0C8
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, bool value)
		{
			KeyenceNanoSerialOverTcp.<WriteAsync>d__13 <WriteAsync>d__ = new KeyenceNanoSerialOverTcp.<WriteAsync>d__13();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<KeyenceNanoSerialOverTcp.<WriteAsync>d__13>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000AED RID: 2797 RVA: 0x00040F1A File Offset: 0x0003F11A
		public override string ToString()
		{
			return string.Format("KeyenceNanoSerialOverTcp[{0}:{1}]", this.IpAddress, this.Port);
		}

		/// <summary>
		/// 建立读取PLC数据的指令，需要传入地址数据，以及读取的长度，地址示例参照类的说明文档<br />
		/// To create a command to read PLC data, you need to pass in the address data, and the length of the read. For an example of the address, refer to the class documentation
		/// </summary>
		/// <param name="address">软元件地址</param>
		/// <param name="length">读取长度</param>
		/// <returns>是否建立成功</returns>
		// Token: 0x06000AEE RID: 2798 RVA: 0x00040F38 File Offset: 0x0003F138
		public static OperateResult<byte[]> BuildReadCommand(string address, ushort length)
		{
			OperateResult<string, int> operateResult = KeyenceNanoSerialOverTcp.KvAnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				bool flag2 = operateResult.Content1 == "CTH" || operateResult.Content1 == "CTC" || operateResult.Content1 == "C" || operateResult.Content1 == "T";
				if (flag2)
				{
					bool flag3 = length > 1;
					if (flag3)
					{
						length /= 2;
					}
				}
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.Append("RDS");
				stringBuilder.Append(" ");
				stringBuilder.Append(operateResult.Content1);
				stringBuilder.Append(operateResult.Content2.ToString());
				stringBuilder.Append(" ");
				stringBuilder.Append(length.ToString());
				stringBuilder.Append("\r");
				byte[] bytes = Encoding.ASCII.GetBytes(stringBuilder.ToString());
				result = OperateResult.CreateSuccessResult<byte[]>(bytes);
			}
			return result;
		}

		/// <summary>
		/// 建立写入PLC数据的指令，需要传入地址数据，以及写入的数据信息，地址示例参照类的说明文档<br />
		/// To create a command to write PLC data, you need to pass in the address data and the written data information. For an example of the address, refer to the class documentation
		/// </summary>
		/// <param name="address">软元件地址</param>
		/// <param name="value">转换后的数据</param>
		/// <returns>是否成功的信息</returns>
		// Token: 0x06000AEF RID: 2799 RVA: 0x0004104C File Offset: 0x0003F24C
		public static OperateResult<byte[]> BuildWriteCommand(string address, byte[] value)
		{
			OperateResult<string, int> operateResult = KeyenceNanoSerialOverTcp.KvAnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.Append("WRS");
				stringBuilder.Append(" ");
				stringBuilder.Append(operateResult.Content1);
				stringBuilder.Append(operateResult.Content2);
				stringBuilder.Append(" ");
				bool flag2 = operateResult.Content1 == "DM" || operateResult.Content1 == "CM" || operateResult.Content1 == "TM" || operateResult.Content1 == "EM" || operateResult.Content1 == "FM" || operateResult.Content1 == "Z";
				if (flag2)
				{
					int num = value.Length / 2;
					stringBuilder.Append(num.ToString());
					stringBuilder.Append(" ");
					for (int i = 0; i < num; i++)
					{
						stringBuilder.Append(BitConverter.ToUInt16(value, i * 2));
						bool flag3 = i != num - 1;
						if (flag3)
						{
							stringBuilder.Append(" ");
						}
					}
				}
				else
				{
					bool flag4 = operateResult.Content1 == "T" || operateResult.Content1 == "C" || operateResult.Content1 == "CTH";
					if (flag4)
					{
						int num2 = value.Length / 4;
						stringBuilder.Append(num2.ToString());
						stringBuilder.Append(" ");
						for (int j = 0; j < num2; j++)
						{
							stringBuilder.Append(BitConverter.ToUInt32(value, j * 4));
							bool flag5 = j != num2 - 1;
							if (flag5)
							{
								stringBuilder.Append(" ");
							}
						}
					}
				}
				stringBuilder.Append("\r");
				result = OperateResult.CreateSuccessResult<byte[]>(Encoding.ASCII.GetBytes(stringBuilder.ToString()));
			}
			return result;
		}

		/// <summary>
		/// 建立写入bool数据的指令，针对地址类型为 R,CR,MR,LR<br />
		/// Create instructions to write bool data, address type is R, CR, MR, LR
		/// </summary>
		/// <param name="address">软元件地址</param>
		/// <param name="value">转换后的数据</param>
		/// <returns>是否成功的信息</returns>
		// Token: 0x06000AF0 RID: 2800 RVA: 0x0004126C File Offset: 0x0003F46C
		public static OperateResult<byte[]> BuildWriteCommand(string address, bool value)
		{
			OperateResult<string, int> operateResult = KeyenceNanoSerialOverTcp.KvAnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				StringBuilder stringBuilder = new StringBuilder();
				if (value)
				{
					stringBuilder.Append("ST");
				}
				else
				{
					stringBuilder.Append("RS");
				}
				stringBuilder.Append(" ");
				stringBuilder.Append(operateResult.Content1);
				stringBuilder.Append(operateResult.Content2);
				stringBuilder.Append("\r");
				result = OperateResult.CreateSuccessResult<byte[]>(Encoding.ASCII.GetBytes(stringBuilder.ToString()));
			}
			return result;
		}

		/// <summary>
		/// 校验读取返回数据状态，主要返回的第一个字节是不是E<br />
		/// Check the status of the data returned from reading, whether the first byte returned is E
		/// </summary>
		/// <param name="ack">反馈信息</param>
		/// <returns>是否成功的信息</returns>
		// Token: 0x06000AF1 RID: 2801 RVA: 0x0004130C File Offset: 0x0003F50C
		public static OperateResult CheckPlcReadResponse(byte[] ack)
		{
			bool flag = ack.Length == 0;
			OperateResult result;
			if (flag)
			{
				result = new OperateResult(StringResources.Language.MelsecFxReceiveZero);
			}
			else
			{
				bool flag2 = ack[0] == 69;
				if (flag2)
				{
					result = new OperateResult(StringResources.Language.MelsecFxAckWrong + " Actual: " + Encoding.ASCII.GetString(ack));
				}
				else
				{
					bool flag3 = ack[ack.Length - 1] != 10 && ack[ack.Length - 2] != 13;
					if (flag3)
					{
						result = new OperateResult(StringResources.Language.MelsecFxAckWrong + " Actual: " + SoftBasic.ByteToHexString(ack, ' '));
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 校验写入返回数据状态，检测返回的数据是不是OK<br />
		/// Verify the status of the returned data written and check whether the returned data is OK
		/// </summary>
		/// <param name="ack">反馈信息</param>
		/// <returns>是否成功的信息</returns>
		// Token: 0x06000AF2 RID: 2802 RVA: 0x000413B4 File Offset: 0x0003F5B4
		public static OperateResult CheckPlcWriteResponse(byte[] ack)
		{
			bool flag = ack.Length == 0;
			OperateResult result;
			if (flag)
			{
				result = new OperateResult(StringResources.Language.MelsecFxReceiveZero);
			}
			else
			{
				bool flag2 = ack[0] == 79 && ack[1] == 75;
				if (flag2)
				{
					result = OperateResult.CreateSuccessResult();
				}
				else
				{
					result = new OperateResult(StringResources.Language.MelsecFxAckWrong + " Actual: " + SoftBasic.ByteToHexString(ack, ' '));
				}
			}
			return result;
		}

		/// <summary>
		/// 从PLC反馈的数据进行提炼Bool操作<br />
		/// Refine Bool operation from data fed back from PLC
		/// </summary>
		/// <param name="addressType">地址的数据类型</param>
		/// <param name="response">PLC反馈的真实数据</param>
		/// <returns>数据提炼后的真实数据</returns>
		// Token: 0x06000AF3 RID: 2803 RVA: 0x00041420 File Offset: 0x0003F620
		public static OperateResult<bool[]> ExtractActualBoolData(string addressType, byte[] response)
		{
			OperateResult<bool[]> result;
			try
			{
				bool flag = string.IsNullOrEmpty(addressType);
				if (flag)
				{
					addressType = "R";
				}
				string @string = Encoding.Default.GetString(response.RemoveLast(2));
				bool flag2 = addressType == "R" || addressType == "CR" || addressType == "MR" || addressType == "LR";
				if (flag2)
				{
					result = OperateResult.CreateSuccessResult<bool[]>(Enumerable.ToArray<bool>(Enumerable.Select<string, bool>(@string.Split(new char[]
					{
						' '
					}, StringSplitOptions.RemoveEmptyEntries), (string m) => m == "1")));
				}
				else
				{
					bool flag3 = addressType == "T" || addressType == "C" || addressType == "CTH" || addressType == "CTC";
					if (flag3)
					{
						result = OperateResult.CreateSuccessResult<bool[]>(Enumerable.ToArray<bool>(Enumerable.Select<string, bool>(@string.Split(new char[]
						{
							' '
						}, StringSplitOptions.RemoveEmptyEntries), (string m) => m.StartsWith("1"))));
					}
					else
					{
						result = new OperateResult<bool[]>(StringResources.Language.NotSupportedDataType);
					}
				}
			}
			catch (Exception ex)
			{
				result = new OperateResult<bool[]>
				{
					Message = string.Concat(new string[]
					{
						"Extract Msg：",
						ex.Message,
						Environment.NewLine,
						"Data: ",
						SoftBasic.ByteToHexString(response)
					})
				};
			}
			return result;
		}

		/// <summary>
		/// 从PLC反馈的数据进行提炼操作<br />
		/// Refining operation from data fed back from PLC
		/// </summary>
		/// <param name="addressType">地址的数据类型</param>
		/// <param name="response">PLC反馈的真实数据</param>
		/// <returns>数据提炼后的真实数据</returns>
		// Token: 0x06000AF4 RID: 2804 RVA: 0x000415D0 File Offset: 0x0003F7D0
		public static OperateResult<byte[]> ExtractActualData(string addressType, byte[] response)
		{
			OperateResult<byte[]> result;
			try
			{
				bool flag = string.IsNullOrEmpty(addressType);
				if (flag)
				{
					addressType = "R";
				}
				string @string = Encoding.Default.GetString(response.RemoveLast(2));
				string[] array = @string.Split(new char[]
				{
					' '
				}, StringSplitOptions.RemoveEmptyEntries);
				bool flag2 = addressType == "DM" || addressType == "CM" || addressType == "TM" || addressType == "EM" || addressType == "FM" || addressType == "Z";
				if (flag2)
				{
					byte[] array2 = new byte[array.Length * 2];
					for (int i = 0; i < array.Length; i++)
					{
						BitConverter.GetBytes(ushort.Parse(array[i])).CopyTo(array2, i * 2);
					}
					result = OperateResult.CreateSuccessResult<byte[]>(array2);
				}
				else
				{
					bool flag3 = addressType == "AT";
					if (flag3)
					{
						byte[] array3 = new byte[array.Length * 4];
						for (int j = 0; j < array.Length; j++)
						{
							BitConverter.GetBytes(uint.Parse(array[j])).CopyTo(array3, j * 4);
						}
						result = OperateResult.CreateSuccessResult<byte[]>(array3);
					}
					else
					{
						bool flag4 = addressType == "T" || addressType == "C" || addressType == "CTH" || addressType == "CTC";
						if (flag4)
						{
							byte[] array4 = new byte[array.Length * 4];
							for (int k = 0; k < array.Length; k++)
							{
								string[] array5 = array[k].Split(new char[]
								{
									','
								}, StringSplitOptions.RemoveEmptyEntries);
								BitConverter.GetBytes(uint.Parse(array5[1])).CopyTo(array4, k * 4);
							}
							result = OperateResult.CreateSuccessResult<byte[]>(array4);
						}
						else
						{
							result = new OperateResult<byte[]>(StringResources.Language.NotSupportedDataType);
						}
					}
				}
			}
			catch (Exception ex)
			{
				result = new OperateResult<byte[]>
				{
					Message = string.Concat(new string[]
					{
						"Extract Msg：",
						ex.Message,
						Environment.NewLine,
						"Data: ",
						SoftBasic.ByteToHexString(response)
					})
				};
			}
			return result;
		}

		/// <summary>
		/// 解析数据地址成不同的Keyence地址类型<br />
		/// Parse data addresses into different keyence address types
		/// </summary>
		/// <param name="address">数据地址</param>
		/// <returns>地址结果对象</returns>
		// Token: 0x06000AF5 RID: 2805 RVA: 0x00041838 File Offset: 0x0003FA38
		public static OperateResult<string, int> KvAnalysisAddress(string address)
		{
			OperateResult<string, int> result;
			try
			{
				bool flag = address.StartsWith("CTH") || address.StartsWith("cth");
				if (flag)
				{
					result = OperateResult.CreateSuccessResult<string, int>("CTH", int.Parse(address.Substring(3)));
				}
				else
				{
					bool flag2 = address.StartsWith("CTC") || address.StartsWith("ctc");
					if (flag2)
					{
						result = OperateResult.CreateSuccessResult<string, int>("CTC", int.Parse(address.Substring(3)));
					}
					else
					{
						bool flag3 = address.StartsWith("CR") || address.StartsWith("cr");
						if (flag3)
						{
							result = OperateResult.CreateSuccessResult<string, int>("CR", int.Parse(address.Substring(2)));
						}
						else
						{
							bool flag4 = address.StartsWith("MR") || address.StartsWith("mr");
							if (flag4)
							{
								result = OperateResult.CreateSuccessResult<string, int>("MR", int.Parse(address.Substring(2)));
							}
							else
							{
								bool flag5 = address.StartsWith("LR") || address.StartsWith("lr");
								if (flag5)
								{
									result = OperateResult.CreateSuccessResult<string, int>("LR", int.Parse(address.Substring(2)));
								}
								else
								{
									bool flag6 = address.StartsWith("DM") || address.StartsWith("DM");
									if (flag6)
									{
										result = OperateResult.CreateSuccessResult<string, int>("DM", int.Parse(address.Substring(2)));
									}
									else
									{
										bool flag7 = address.StartsWith("CM") || address.StartsWith("cm");
										if (flag7)
										{
											result = OperateResult.CreateSuccessResult<string, int>("CM", int.Parse(address.Substring(2)));
										}
										else
										{
											bool flag8 = address.StartsWith("TM") || address.StartsWith("tm");
											if (flag8)
											{
												result = OperateResult.CreateSuccessResult<string, int>("TM", int.Parse(address.Substring(2)));
											}
											else
											{
												bool flag9 = address.StartsWith("EM") || address.StartsWith("em");
												if (flag9)
												{
													result = OperateResult.CreateSuccessResult<string, int>("EM", int.Parse(address.Substring(2)));
												}
												else
												{
													bool flag10 = address.StartsWith("FM") || address.StartsWith("fm");
													if (flag10)
													{
														result = OperateResult.CreateSuccessResult<string, int>("FM", int.Parse(address.Substring(2)));
													}
													else
													{
														bool flag11 = address.StartsWith("AT") || address.StartsWith("at");
														if (flag11)
														{
															result = OperateResult.CreateSuccessResult<string, int>("AT", int.Parse(address.Substring(2)));
														}
														else
														{
															bool flag12 = address.StartsWith("Z") || address.StartsWith("z");
															if (flag12)
															{
																result = OperateResult.CreateSuccessResult<string, int>("Z", int.Parse(address.Substring(1)));
															}
															else
															{
																bool flag13 = address.StartsWith("R") || address.StartsWith("r");
																if (flag13)
																{
																	result = OperateResult.CreateSuccessResult<string, int>("", int.Parse(address.Substring(1)));
																}
																else
																{
																	bool flag14 = address.StartsWith("T") || address.StartsWith("t");
																	if (flag14)
																	{
																		result = OperateResult.CreateSuccessResult<string, int>("T", int.Parse(address.Substring(1)));
																	}
																	else
																	{
																		bool flag15 = address.StartsWith("C") || address.StartsWith("c");
																		if (!flag15)
																		{
																			throw new Exception(StringResources.Language.NotSupportedDataType);
																		}
																		result = OperateResult.CreateSuccessResult<string, int>("C", int.Parse(address.Substring(1)));
																	}
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				result = new OperateResult<string, int>(ex.Message);
			}
			return result;
		}

		/// <summary>
		/// 连接PLC的命令报文<br />
		/// Command message to connect to PLC
		/// </summary>
		// Token: 0x04000322 RID: 802
		public static byte[] ConnectCmd = new byte[]
		{
			67,
			82,
			13
		};

		/// <summary>
		/// 断开PLC连接的命令报文<br />
		/// Command message to disconnect PLC
		/// </summary>
		// Token: 0x04000323 RID: 803
		public static byte[] DisConnectCmd = new byte[]
		{
			67,
			81,
			13
		};
	}
}
